import { useState, useEffect } from 'react'
import { FetchRes, useFetch } from '@/plugins/axios'

export type userFetchRes = {
	/**用户名 */
	name: string
	/**头像url 32*32 */
}

type userFetch = Required<FetchRes<userFetchRes>>
//
export const useUser = () => {
	const [userInfo, setUserInfo] = useState<userFetchRes>()
	//获取用户信息
	const [{ loading, data }] = useFetch<userFetch>(
		{
			url: '/get_user_info',
			method: 'get',
		},
		{
			cacheKey: 'userInfo',
			cacheTime: Infinity,
		}
	)
	useEffect(() => {
		if (!loading) {
			if (data && data.success && data.data) {
				setUserInfo(data.data)
			}
		}
	}, [data, loading])

	return { userInfo }
}
